//
//  BVUser.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/12.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVUser.h"

#define USER_DEFAULTS_USER_ID @"user_id"
#define USER_DEFAULTS_NAME @"name"
#define USER_DETAULTS_MOBILE @"mobile"
#define USER_DEFAULTS_GENDER @"gender"
#define USER_DEFAULTS_ADDRESS @"address"
#define USER_DEFAULTS_DETAIL_ADDRESS @"detail_address"
#define USER_DEFAULTS_BIRTHDAY @"birthday"
#define USER_DEFAULTS_CITY @"city"
#define USER_DEFAULTS_CITY_ID @"city_id"
#define USER_DEFAULTS_AVATARURL @"avatar_url"

#define USER_DEFAULTS_ADDR_PROVINCEID @"addr_provinceId"
#define USER_DEFAULTS_ADDR_CITYID @"addr_cityId"

@interface BVUser()

@property (nonatomic, strong)NSUserDefaults *userDefaults;

@end


@implementation BVUser

#pragma mark -
#pragma mark - Singleton User
- (NSUserDefaults *)userDefaults {
    if (!_userDefaults) {
        _userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return _userDefaults;
}

+ (BVUser *)sharedUser {
    static BVUser *sharedUserInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedUserInstance = [[BVUser alloc] init];
    });
    return sharedUserInstance;
}

#pragma mark -
#pragma mark Property Setter And Getter
- (NSString *)user_id {
    return [self.userDefaults objectForKey:USER_DEFAULTS_USER_ID];
}

- (void)setUser_id:(NSString *)user_id {
    [self.userDefaults setObject:user_id forKey:USER_DEFAULTS_USER_ID];
}

- (NSString *)name {
    return [self.userDefaults objectForKey:USER_DEFAULTS_NAME];
}

- (void)setName:(NSString *)name {
    [self.userDefaults setObject:name forKey:USER_DEFAULTS_NAME];
}

- (NSString *)mobile {
    return [self.userDefaults objectForKey:USER_DETAULTS_MOBILE];
}

- (void)setMobile:(NSString *)mobile {
    [self.userDefaults setObject:mobile forKey:USER_DETAULTS_MOBILE];
}

- (NSInteger)gender {
    return [self.userDefaults integerForKey:USER_DEFAULTS_GENDER];
}

- (void)setGender:(NSInteger)gender {
    [self.userDefaults setInteger:gender forKey:USER_DEFAULTS_GENDER];
}

- (NSString *)address {
    return [self.userDefaults objectForKey:USER_DEFAULTS_ADDRESS];
}

- (void)setAddress:(NSString *)address {
    [self.userDefaults setObject:address forKey:USER_DEFAULTS_ADDRESS];
}

- (void)setDetail_address:(NSString *)detail_address {
    [self.userDefaults setObject:detail_address forKey:USER_DEFAULTS_DETAIL_ADDRESS];
}

- (NSString *)detail_address {
    return [self.userDefaults objectForKey:USER_DEFAULTS_DETAIL_ADDRESS];
}

- (void)setBirthday:(NSString *)birthday {
    [self.userDefaults setObject:birthday forKey:USER_DEFAULTS_BIRTHDAY];
}

- (NSString *)birthday {
    return [self.userDefaults objectForKey:USER_DEFAULTS_BIRTHDAY];
}

- (NSString *)avatar_url {
    return [self.userDefaults objectForKey:USER_DEFAULTS_AVATARURL];
}
- (void)setAvatar_url:(NSString *)avatar_url {
    [self.userDefaults setObject:avatar_url forKey:USER_DEFAULTS_AVATARURL];
}

- (NSString *)city {
    return [self.userDefaults objectForKey:USER_DEFAULTS_CITY];
}

//- (void)setCity:(NSString *)city {
//    [self.userDefaults setObject:city forKey:USER_DEFAULTS_CITY];
//}

- (NSString *)city_id {
    return [self.userDefaults objectForKey:USER_DEFAULTS_CITY_ID];
}

//- (void)setCity_id:(NSString *)city_id {
//    [self.userDefaults setObject:city_id forKey:USER_DEFAULTS_CITY_ID];
//}

- (void)setCity:(NSString *)city city_id:(NSString *)city_id {
    [self.userDefaults setObject:city forKey:USER_DEFAULTS_CITY];
    [self.userDefaults setObject:city_id forKey:USER_DEFAULTS_CITY_ID];
}

// 家庭地址—城市id
- (NSString *)addr_cityId {
    return [self.userDefaults objectForKey:USER_DEFAULTS_ADDR_CITYID];
}
- (void)setAddr_cityId:(NSString *)addr_cityId {
    [self.userDefaults setObject:addr_cityId forKey:USER_DEFAULTS_ADDR_CITYID];
}

// 家庭地址-省份id
- (NSString *)addr_provinceId {
    return [self.userDefaults objectForKey:USER_DEFAULTS_ADDR_PROVINCEID];
}
- (void)setAddr_provinceId:(NSString *)addr_provinceId {
    [self.userDefaults setObject:addr_provinceId forKey:USER_DEFAULTS_ADDR_PROVINCEID];
}


- (BOOL)isLogin {
    return self.user_id ? YES : NO;
}

- (void)logout {
    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_USER_ID];
    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_NAME];
    [self.userDefaults setObject:nil forKey:USER_DETAULTS_MOBILE];
    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_GENDER];
    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_ADDRESS];
//    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_CITY];
//    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_CITY_ID];
    [self.userDefaults setObject:nil forKey:USER_DEFAULTS_AVATARURL];
}

@end
